home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / B-C / C Servant.sit / C Servant™.rsrc / TEXT_150_aText.txt < prev    next >
Encoding:
Text File  |  1992-02-23  |  1.9 KB  |  32 lines

  1.  
  2. 23. #define, #include
  3.  
  4.             C provides a very limited macro facility. You can say
  5.  
  6.             #define name something
  7.  
  8. and thereafter anywhere ``name‚Äô‚Äô appears as a token, ``something‚Äô‚Äô will be substituted. This is particularly useful in parametering the sizes of arrays:
  9.  
  10.             #define ARRAYSIZE 100
  11.                         int arr[ARRAYSIZE];
  12.                         ...
  13.                         while( i++ < ARRAYSIZE )...
  14.  
  15. (now we can alter the entire program by changing only the define) or in setting up mysterious constants:
  16.  
  17.             #define SET 01
  18.             #define INTERRUPT 02 /* interrupt bit */
  19.             #define ENABLED 04
  20.             ...
  21.             if( x & (SET | INTERRUPT | ENABLED) ) ...
  22.  
  23. Now we have meaningful words instead of mysterious constants. (The mysterious operators `&‚Äô (AND) and `|‚Äô (OR) will be covered in the next section.) It‚Äôs an excellent practice to write programs without any literal constants except in #define statements.
  24.  
  25.             There are several warnings about #define. First, there‚Äôs no semicolon at the end of a #define; all the text from the name to the end of the line (except for comments) is taken to be the ``something‚Äô‚Äô. When it‚Äôs put into the text, blanks are placed around it. Good style typically makes the name in the #define upper case‚Äîthis makes parameters more visible. Definitions affect things only after they occur, and only within the file in which they occur. Defines can‚Äôt be nested. Last, if there is a #define in a file, then the first character of the file MUST be a `#‚Äô, to signal the preprocessor that definitions exist.
  26.  
  27.             The other control word known to C is #include. To include one file in your source at compilation time, say
  28.  
  29.             #include ‚Äúfilename‚Äù
  30.  
  31. This is useful for putting a lot of heavily used data definitions and #define statements at the beginning of a file to be compiled. As with #define, the first line of a file containing a #include has to begin with a `#‚Äô. And #include can‚Äôt be nested‚Äî an included file can‚Äôt contain another #include.
  32.